package com.homedo.microservice.open.service.gateway.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.bean.enums.forzuulfilter.ProgressEnum4ZuulFilter;
import com.homedo.microservice.open.service.gateway.bean.enums.forzuulfilter.StatusEnum4ZuulFilter;
import com.homedo.microservice.open.service.gateway.bean.po.ZuulFilter;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.ConfigEnhanced;
import com.homedo.microservice.open.service.gateway.bean.po.enhanced.ZuulFilterEnhanced;
import com.homedo.microservice.open.service.gateway.persistence.dao.ZuulFilterDao;
import com.homedo.microservice.open.service.gateway.zuul.conf.ZuulCommonConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月10日 16时28分40秒
 */
@Service
public class ZuulFilterService extends BaseService<ZuulFilter> {
    @Autowired
    private ZuulFilterDao zuulFilterDao;
    @Autowired
    private ConfigService configService;
    @Autowired
    private ZuulCommonConf zuulCommonConf;


    @Override
    public List<ZuulFilterEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<ZuulFilter> zuulFilters = zuulFilterDao.getListByIds(ids, ZuulFilter.class);

        if (!Detect.notEmpty(zuulFilters)) {
            return null;
        }

        List<ZuulFilterEnhanced> zuulFilterEnhanceds = this.convent2Enhanceds(zuulFilters);
        this.appendZuulFilterDisableConfEnhanced(zuulFilterEnhanceds, userContext);

        return zuulFilterEnhanceds;
    }


    @Override
    public List<ZuulFilterEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<ZuulFilter> zuulFilters = zuulFilterDao.getListByIdsGreaterThanMark0(ids, ZuulFilter.class);

        if (!Detect.notEmpty(zuulFilters)) {
            return null;
        }

        List<ZuulFilterEnhanced> zuulFilterEnhanceds = this.convent2Enhanceds(zuulFilters);
        this.appendZuulFilterDisableConfEnhanced(zuulFilterEnhanceds, userContext);

        return zuulFilterEnhanceds;
    }


    @Override
    public List<ZuulFilterEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<ZuulFilter> zuulFilters = (List<ZuulFilter>) pos;
        List<ZuulFilterEnhanced> zuulFilterEnhanceds = Lists.newArrayList();
        zuulFilters.forEach(e -> zuulFilterEnhanceds.add(new ZuulFilterEnhanced(e)));
        return zuulFilterEnhanceds;
    }


    public List<ZuulFilterEnhanced> getEnhanceds4Application(String applicationName, List<ProgressEnum4ZuulFilter> progresses, List<StatusEnum4ZuulFilter> statuses, UserContext userContext) {
        List<ZuulFilter> zuulFilters = zuulFilterDao.getList4Application(applicationName, progresses, statuses);

        if (!Detect.notEmpty(zuulFilters)) {
            return null;
        }
        List<ZuulFilterEnhanced> zuulFilterEnhanceds = this.convent2Enhanceds(zuulFilters);
        this.appendZuulFilterDisableConfEnhanced(zuulFilterEnhanceds, userContext);

        return zuulFilterEnhanceds;
    }

    public List<ZuulFilterEnhanced> appendZuulFilterDisableConfEnhanced(List<ZuulFilterEnhanced> zuulFilterEnhanceds, UserContext userContext) {
        List<String> disablePropertyNames = zuulFilterEnhanceds.stream().map(ZuulFilterEnhanced::getDisablePropertyName).collect(Collectors.toList());

        List<ConfigEnhanced> configEnhanceds = configService.getEnhanceds4Application(zuulCommonConf.getApplicationName(), disablePropertyNames, userContext);

        if (!Detect.notEmpty(configEnhanceds)) {
            return zuulFilterEnhanceds;
        }

        this.appendZuulFilterDisableConfEnhanced(zuulFilterEnhanceds, configEnhanceds, userContext);

        return zuulFilterEnhanceds;
    }

    private void appendZuulFilterDisableConfEnhanced(List<ZuulFilterEnhanced> zuulFilterEnhanceds, List<ConfigEnhanced> configEnhanceds, UserContext userContext) {
        zuulFilterEnhanceds.forEach(e -> {
            this.appendZuulFilterDisableConfEnhanced(e, configEnhanceds, userContext);
        });
    }


    private void appendZuulFilterDisableConfEnhanced(ZuulFilterEnhanced zuulFilterEnhanced, List<ConfigEnhanced> configEnhanceds, UserContext userContext) {
        configEnhanceds.forEach(e -> {
            if (e.getApplicationName().equals(zuulFilterEnhanced.getApplicationName()) && e.getName().equals(zuulFilterEnhanced.getDisablePropertyName())) {
                zuulFilterEnhanced.setZuulFilterDisableConfEnhanced(e);
                return;
            }
        });
    }


}
